// pages/material/index.js
const controllers = require('../controllers/controllerIndex.js').controller;
const BASE_URL = require('../../utils/kzj.request.js').url;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    src1:" https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/my/img/my_but_15.png",
    src2:" https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/my/img/my_but_26.png",
    list: [],
    dialogVisible: false,
    selItem: {
      image: '',
      state: 0,
      goodsId: '',
      name: '',
      url: '',
      link:"",
      ids:""
    },
    states: [
      {
        value: 0,
        text: '不跳转'
      },
      {
        value: 1,
        text: '商品/拼团'
      },
      // {
      //   value: 2,
      //   text: '外链'
      // },
      {
        value: 4,
        text: '优惠券'
      },
      {
        value: 3,
        text: 'vip卡'
      }
    ],
    goods: [
      '商品1',
      '商品2'
    ],
    chooseProduct: [],
    saving: false,
    chooseCoupon:""
  },
  onLoad: function (options) {
    this.shopRotationImg()
  },
  onShow: function () {
    var that = this;
    var chooseProduct = that.data.chooseProduct;
    let chooseCoupon = that.data.chooseCoupon;
    let chooseCard = that.data.chooseCard;
    console.log('onShow--chooseProduct==', chooseProduct);
    if (chooseProduct && chooseProduct.length) {
      that.data.selItem.goodsId = chooseProduct[0].id;
      that.data.selItem.name = chooseProduct[0].name;
      that.setData({
        selItem: that.data.selItem
      })
    }
    if (chooseCoupon) {
      console.log("执行")
      that.data.selItem.ids = chooseCoupon.ids;
      that.data.selItem.name = chooseCoupon.name;
      that.data.selItem.url = "/pages/Mall/Coupondetail";
      that.data.selItem.state = 4;
      that.setData({
        selItem: that.data.selItem
      })
    }
    if (chooseCard) {
      console.log("执行")
      that.data.selItem.ids = chooseCard.ids;
      that.data.selItem.name = chooseCard.name;
      that.data.selItem.state = 3;
      that.data.selItem.url = "/pages/my/vip/info/VIPCardInfo";
      that.setData({
        selItem: that.data.selItem
      })
    }
  },

  switchDialog(e) {
    console.log('switchDialog--e==', e);
    var that = this;
    var type = e.currentTarget.dataset.type;
    if (type === "new") {
      that.data.dialogVisible = true;
      that.data.selItem = {};
    } else if (type === "cancel") {
      that.data.dialogVisible = false;
      that.data.selItem = {};
    } else  if (type === "edit") {
      var index = e.currentTarget.dataset.index;
      that.data.dialogVisible = true;
      that.data.selItem = that.data.list[index];
    }
    that.setData({
      dialogVisible: that.data.dialogVisible,
      selItem: that.data.selItem
    })
  },
  delImg(e) {
    var that = this;
    const id = e.currentTarget.dataset.id;
    const shopId = wx.getStorageSync('shopId');
    if (that.data.saving) {
      return;
    }
    that.data.saving = true;
    wx.showLoading();
    controllers.delImg({ id, shopId }).then(res => {
      console.log('delImg--res==', res);
      const { code, data, desc } = res;
      that.data.saving = false;
      that.setData({
        dialogVisible: false,
        selTtem: {}
      })
      that.shopRotationImg();
      wx.hideLoading();
    }).catch(res => {
      that.data.saving = false;
      wx.hideLoading();
    })
  },
  shopRotationImg() {
    var that = this;
    const params = {
      shopId:wx.getStorageSync('shopId'),
      type: 6
    };
    controllers.shopRotationImg(params).then(res => {
      console.log('shopRotationImg--res==', res);
      const { code, data, desc } = res;
      that.setData({
        list: data
      })
    })
  },
  updateShopIndex(params) {
    var that = this;
    controllers.updateShopIndex(params).then(res => {
      console.log('updateShopIndex--res==', res);
      const { code, data, desc } = res;
      that.data.saving = false;
      that.setData({
        dialogVisible: false,
        selTtem: {}
      })
      that.shopRotationImg();
      wx.hideLoading();
    }).catch(res => {
      that.data.saving = false;
      wx.hideLoading();
    })
  },
  saveShopIndex(params) {
    var that = this;
    controllers.saveShopIndex(params).then(res => {
      console.log('saveShopIndex--res==', res);
      const { code, data, desc } = res;
      that.setData({
        dialogVisible: false,
        selTtem: {},
        saving: false
      })
      that.shopRotationImg();
      wx.hideLoading();
    }).catch(res => {
      that.data.saving = false;
      wx.hideLoading();
    })
  },
  sub() {
    var that = this;
    var selItem = that.data.selItem;
    var showToast = false;
    var title = '';
    var params = { ...selItem };
    console.log('sub--params==', params);
    
    if (!selItem) {
      title = '请录入数据';
      showToast = true;
    } else if (!selItem.image) {
      title = '请上传图片';
      showToast = true;
    } else if (!selItem.state && selItem.state !== 0) {
      title = '请先进行跳转效果设置';
      showToast = true;
    } else if (selItem.state === 1) {
      params.url = '';
      if (!selItem.name || !selItem.goodsId) {
        title = '请选择跳转商品';
        showToast = true;
      }
    } 
    // else if (selItem.state === 2) {
    //   params.name = '';
    //   if (!selItem.url) {
    //     title = '请输入外链链接';
    //     showToast = true;
    //   }
    // }
    if (showToast) {
      wx.showToast({
        title,
        icon: 'none'
      })
      return;
    }
    if (that.data.saving) {
      return;
    }
    that.data.saving = true;
    wx.showLoading()
    if (params.id) {
      that.updateShopIndex(params);
    } else {
      that.saveShopIndex(params);
    }
  },
  closeImg() {
    var that = this;
    that.data.selItem.image = '';
    that.setData({
      selItem: that.data.selItem
    })
  },
  choose(){
    let that=this;
    wx.chooseImage({
      success (res) {
        const tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: BASE_URL + '/api/img/uploadFile', 
          filePath: tempFilePaths[0],
          name: 'image',
          formData: {
            'Content-Type': 'application/form-data',
            'sessionId': wx.getStorageSync('token')
          },
          success (res){
            console.log('uploadFile--res==', res);
            let src=JSON.parse(res.data)
            that.data.selItem.image = src.data;
            that.setData({
              selItem: that.data.selItem
            })
          }
        })
      }
    })
  },
  bindPickerChange(e) {
    console.log('bindPickerChange--e==', e);
    var value = parseInt(e.detail.value);
    that.data.selItem.goodsIndex = value;
    that.setData({
      selItem: that.data.selItem
    })
  },
  getInput(e) {
    var that = this;
    console.log('getInput--e==', e);
    var name = e.currentTarget.dataset.name;
    var value = e.detail.value;
    that.data.selItem[name] = value;
    that.setData({
      selItem: that.data.selItem
    })
  },
  changeType(e) {
    var that = this;
    var value = e.currentTarget.dataset.value;
    if (value !== that.data.selItem.states) {
      that.data.selItem.state = value;
      that.setData({
        selItem: that.data.selItem
      })
    }
  },
  openPage(e) {
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }
})